// pages/apply/apply.js
var app = getApp();
var until = require('../../utils/util.js')
var index = require('../index/index.js')
Page({
  data: {
    invi:'',
    user: '',
    gradeArr: ['1', '2', '3', '4'],
    gradeObjectArray: [
      {
        id: 0,
        name: '1'
      },
      {
        id: 1,
        name: '2'
      },
      {
        id: 2,
        name: '3'
      },
      {
        id: 3,
        name: '4'
      }
    ],
    typeArr: ['yi', 'er', 'san', 'si'],
    typeObjectArray: [
      {
        id: 0,
        name: 'yi'
      },
      {
        id: 1,
        name: 'er'
      },
      {
        id: 2,
        name: 'san'
      },
      {
        id: 3,
        name: 'si'
      }
    ],
    sexItem: [
      { name: '男', value: '男', checked: 'true' },
      { name: '女', value: '女' },
    ],
    maxPhoNum: 1,
    imageList: [],
    showViewpls: 1,
    showViewgrd: 0,
    delIcon: "../../img/del.png",
    pid: "",
    discount: '',
  },
  reset:function(){
    this.setData({
      sexItem: [
        { name: '男', value: '男', checked: 'true' },
        { name: '女', value: '女' },
      ]
    })
  },
  gradeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      gradeIndex: e.detail.value
    })
  },
  typeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      typeIndex: e.detail.value,
      showViewgrd: 1
    })
  },



  radioChange: function (e) {
    var that = this
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var sex = "user.sex"
    that.setData({
      [sex]: e.detail.value
    })
    console.log(this.data.user)
  },
  chooseImage: function () {
    var that = this
    var imgli = that.data.imageList
    wx.chooseImage({
      sourceType: ['album', 'camera'],
      sizeType: 'original',
      count: 1,
      success: function (res) {
        // console.log("触发了up")
        // console.log(res.tempFilePaths)
        var listTmp = res.tempFilePaths
        that.setData({
          imageList: listTmp
        })
        // var formData = new FormData();
        if (that.data.imageList.length >= 1) {
          that.setData({
            showViewpls: 0,
            // showViewdel: 1
          })
        }
        // console.log(that.data.imageList)
        // console.log(that.data.showView)
        var tempFilePaths = res.tempFilePaths
        that.setData({
          tempFilePaths: tempFilePaths
        })
        console.log(that.data.tempFilePaths)
      }
    })
  },
  uploadimg: function () {//这里触发图片上传的方法
    console.log(this.data.user)
    // wx.uploadFile({
    //   url: 'https://selwyn.mynatapp.cc/user/up', //仅为示例，非真实的接口地址
    //   filePath: tempFilePaths[0],
    //   name: 'file',
    //   formData: {
    //     'user': 'test'
    //   },
    //   success: function (res) {
    //     var data = res.data
    //     //do something
    //   }
    // })
  },
  previewImage: function (e) {
    var current = e.target.dataset.src
    console.log(current)
    wx.previewImage({
      current: current,
      urls: this.data.imageList
    })
  },
  deleteImage: function (e) {
    var that = this
    console.log(e.currentTarget.dataset.index)
    var _index = e.currentTarget.dataset.index
    var imageListNew = that.data.imageList;
    imageListNew.splice(_index, 1);
    that.setData({
      imageList: imageListNew
    })
    if (that.data.imageList.length <= that.data.maxPhoNum - 1) {
      that.setData({
        // showViewdel: 0,
        showViewpls: 1,
      })
    }
  },
  checkINVI: function (e) {
    // console.log(e.detail.value)
    var that = this
    var data = e.detail.value
    wx.request({
      url: 'https://selwyn.mynatapp.cc/user/findOneByInvi', //仅为示例，并非真实的接口地址
      data: {
        inviCode: data
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'post',
      success: function (res) {
        console.log(res.data)
        if (res.data.length > 0) {
          wx.showToast({
            title: '邀请码正确',
            icon: 'success',
            duration: 2000
          })
          var id = res.data[0].id
          that.setData({ pid: id })
          // console.log(that.data.pid)
        }
        else {
          wx.showToast({
            title: '邀请码无效',
            icon: "none",
            duration: 2000
          })
        }
      }
    })
    console.log(this.data.pid)
  },
  formSubmit: function (e) {
    var that = this
    // console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var data = e.detail.value
    var date = new Date().getTime()
    data.pid = this.data.pid
    data.creationTime = date
    data.status = 0
    data.inviCode = until.guidInvi()
    data.icon = until.guid()
    data.discount = this.data.discount
    data.password = '1111'
    that.setData({
      user: data
    })
    if (data.name == "" || data.age == "" || data.address == "") {
      wx.showToast({
        title: '信息不全',
        icon: 'none',
        duration: 2000
      })
    }
    else {
      var path = that.data.tempFilePaths
      path = path[0]
      var user = that.data.user
      console.log(user)
      wx.uploadFile({
        url: 'https://selwyn.mynatapp.cc/user/up', //仅为示例，非真实的接口地址
        filePath: path,
        name: 'file',
        formData: user,
        header: { "chartset": "utf-8" },
        success: function (res) {
          var data = res.data
          wx.showToast({
            title: '提交成功',
            icon: 'success',
            duration: 2000
          })
        },
        error: function (res) {
          wx.showToast({
            title: '提交失败',
            icon: 'none',
            duration: 2000
          })
          console.log(res)
        }
      })
    }
    // console.log(this.data.tempFilePaths)
    // wx.request({
    //   url: 'https://selwyn.mynatapp.cc/user/addUserAction', //仅为示例，并非真实的接口地址
    //   data: data,
    //   header: {
    //     'content-type': 'application/json' // 默认值
    //   },
    //   method: 'post',
    //   success: function (res) {
    //     console.log(res.data)
    //     if (res.data.msg !='msg') {
    //       wx.showToast({
    //         title: '成功',
    //         icon: 'success',
    //         duration: 2000
    //       })
    //     }
    //     else {
    //       that.setData({ showView: 1 });
    //       var fadeOutTimeout = setTimeout(() => {
    //         that.setData({ showView: 0 });
    //         clearTimeout(fadeOutTimeout);
    //       }, 2000);
    //     }
    //   }
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.code)
    this.setData({
      invi:options.code
    })
    // console.log(index.data.userInfo.inviCode)
    var that = this
    wx.request({
      url: 'https://selwyn.mynatapp.cc/order/findDiscount', //仅为示例，并非真实的接口地址
      method: 'POST',
      success: function (res) {
        that.setData({
          discount: res.data[0].discount
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    console.log(this.data.discount)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: index.data.userInfo.name+'邀请你成为代理',
      path: 'pages/apply/apply?code='+index.data.userInfo.inviCode,
      success: function (res) {
        // 转发成功
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail: function (res) {
        // 转发失败
        wx.showToast({
          title: '提交失败',
          icon: 'none',
          duration: 2000
        })
      }
    }
  }
})